﻿Imports TepcelBL
Imports TepcelDominio
Public Class frmAgregarProducto
    Dim ListaExistencia As List(Of Existencia)
    Dim ExistenciaBL As ExistenciaBL
    Dim texto As String
    Public Property row As DataGridViewRow


    Private Sub btnCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        Me.Close()
    End Sub

    Private Sub frmAgregarProducto_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
        txtBuscar.Focus()
    End Sub

    Private Sub frmAgregarProducto_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        ExistenciaBL = Nothing
    End Sub

    Private Sub frmAgregarProducto_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ExistenciaBL = New ExistenciaBL
        texto = row.Cells(2).EditedFormattedValue
        txtBuscar.Text = texto
        txtBuscar.SelectionStart = texto.Length
        txtBuscar.SelectionLength = 0

        DGVListaProductos.Rows.Clear()
        If texto.Trim.Length = 0 Then
            ListaExistencia = ExistenciaBL.GetAllFromExistencia
        End If
        Call llenarGrid()
    End Sub

    Private Sub llenarGrid()
        If ListaExistencia.Count = 0 Then
            btnAceptar.Enabled = False
        Else
            btnAceptar.Enabled = True
        End If
        Dim i As Integer
        For i = 0 To ListaExistencia.Count - 1
            With ListaExistencia.Item(i)
                DGVListaProductos.Rows.Add(.Producto.codigoProducto, .Producto.nombreProducto, .Producto.descripcionProducto, .stockProducto)
            End With
        Next
    End Sub

    Private Sub txtBuscar_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtBuscar.KeyDown
        With DGVListaProductos
            If DGVListaProductos.Rows.Count = 0 Then Return
            Select Case e.KeyData
                Case Keys.Down
                    e.Handled = True
                    If .CurrentRow.Index < .Rows.Count - 1 Then
                        .Rows(.CurrentRow.Index + 1).Selected = True
                        .CurrentCell = .Rows(.CurrentRow.Index + 1).Cells(0)
                    Else
                        .Rows(0).Selected = True
                        .CurrentCell = .Rows(0).Cells(0)
                    End If
                Case Keys.Up
                    e.Handled = True
                    If .CurrentRow.Index > 0 Then
                        .Rows(.CurrentRow.Index - 1).Selected = True
                        .CurrentCell = .Rows(.CurrentRow.Index - 1).Cells(0)
                    Else
                        .Rows(.Rows.Count - 1).Selected = True
                        .CurrentCell = .Rows(.Rows.Count - 1).Cells(0)
                    End If
                Case Keys.Enter
                    Call AgregarProducto()

                Case Keys.Escape
                    Me.Close()

            End Select

        End With

    End Sub


    Private Sub txtBuscar_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBuscar.TextChanged
        DGVListaProductos.Rows.Clear()
        ListaExistencia = ExistenciaBL.SearchFromExistencia(txtBuscar.Text.Trim)
        Call llenarGrid()

    End Sub

    Private Sub AgregarProducto()
        If DGVListaProductos.Rows.Count = 0 Then Return
        If DGVListaProductos.CurrentRow Is Nothing Then Return
        If DGVListaProductos.CurrentRow.Cells(3).Value <= 0 And frmMovimientoAlmacen.tipoNota = "S" Then
            MsgBox("No hay Stock Disponible")
            Return
        End If
        Dim i As Integer
        With frmMovimientoAlmacen.Instance().DGVLista
            For i = 0 To .Rows.Count - 1
                If i <> .CurrentRow.Index Then
                    If .Rows(i).Cells(1).Value = DGVListaProductos.Rows(DGVListaProductos.CurrentRow.Index).Cells(0).Value Then
                        MsgBox("El producto que intenta agergar ya esta en la lista")
                        Return
                    End If
                End If
            Next
        End With

        row.Cells(1).Value = DGVListaProductos.CurrentRow.Cells(0).Value
        row.Cells(2).Value = DGVListaProductos.CurrentRow.Cells(1).Value
        row.Cells(0).Value = DGVListaProductos.CurrentRow.Cells(3).Value
        Dim detalle As New NotaAlmacen.DetalleNotaAlmacen
        detalle.Producto.codigoProducto = row.Cells(1).Value
        detalle.Producto.nombreProducto = row.Cells(2).Value

        frmMovimientoAlmacen.Instance().NotaAlmacen.DetalleAlmacen.Add(detalle)
        detalle = Nothing
        Me.Close()
    End Sub

    Private Sub btnAceptar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAceptar.Click
        Call AgregarProducto()
    End Sub
End Class